// pages/allCourseList/allCourseList.js
const app = getApp()
const httpUtils = require('../../utils/httpUtils.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: 'https://material.sse100.cn/shop/',
    msg: true,
    categray_type: -1, //0 王牌单词 1 精品课程
    grade: -1, //年级 1 小学 2 初中 3 高中 4 大学 5 其他
    version: -1, //版本
    tempcategray_type:-1, //0 王牌单词 1 精品课程
    tempgrade: -1, //年级 1 小学 2 初中 3 高中 4 大学 5 其他
    tempversion:-1, //版本
    categray_con: [], //类型
    grade_con: [], //单词年级
    versionlist: [], //类型
    chooseData: [], //弹框显示的内容
    chooseCurrentPosition: -1, //当前选中的id
    chooseType: 0, //目前正在选择的类型1是类型2年级3版本
    chooseCategrayTypeText: "类型",
    chooseGradeText: "年级",
    chooseVersionText: "版本",
    lists: null //课程列表
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  getData: function () {
    let that = this
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    httpUtils.courselist({
      access_token: app.globalData.$TOKEN,
      categray_type: that.data.categray_type
    }).then((res) => {
      wx.hideLoading()
      that.setData({
        categray_con: res.data.categray_con,
        grade_con: res.data.grade_con,
        lists: res.data.lists,
      });
    });


  },
  getChooseData: function () {
    let that = this
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    httpUtils.courselist({
      access_token: app.globalData.$TOKEN,
      categray_type: that.data.categray_type,
      grade: that.data.grade,
      version: that.data.version
    }).then((res) => {
      wx.hideLoading()
      that.setData({
        categray_con: res.data.categray_con,
        grade_con: res.data.grade_con,
        lists: res.data.lists,
        chooseType:0,
        msg: true
      });
    });


  },
  getVersion: function () {
    let that = this
    wx.showLoading({
      title: '正在加载',
      mask: true
    })

    httpUtils.getVersion({
      categray: that.data.tempcategray_type,
      grade: that.data.tempgrade,
    }).then((res) => {
      wx.hideLoading()
      that.setData({
        versionlist: res.data,
        chooseCurrentPosition: -1,
        versionlist: res.data,
        chooseData: res.data,
        chooseType: 3
      });
    });


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	let course_type = wx.getStorageSync('course_type');
	if (course_type >= 0) {
	  this.setData({
	    categray_type: course_type
	  })
	}
	this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
	  this.setData({
	    categray_type: -1
	  })
    this.bindMaskClick();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindMaskClick: function () {
    //点击阴影部分
    console.log("点击阴影部分");


    this.setData({
      msg: true,
      tempcategray_type: this.data.categray_type,
      tempgrade: this.data.grade,
      tempversion: this.data.version,
      chooseType:0,
    })
  },
  /**
   * 点击选择的内容
   */
  chooseChooseType: function (e) {

    let that = this;
    var data = e.currentTarget.dataset['id'];
    var id = data.id;
    var title = data.title
  
    that.setData({
      chooseCurrentPosition: id,

    })
    setTimeout(function () {
      if (that.data.chooseType == 1) {
        that.setData({
          tempcategray_type: id,
          grade: that.data.grade_con[id],
          tempgrade: -1,
          tempversion: -1,
          chooseCurrentPosition: that.data.grade,
          chooseData: that.data.grade_con[id],
          chooseCategrayTypeText: title,
          chooseGradeText: "年级",
          chooseVersionText: "版本",
          chooseType: 2
        })


      } else if (that.data.chooseType == 2) {
        that.setData({
          tempgrade: id,
          tempversion: -1,
          chooseGradeText:title,
          chooseVersionText: "版本",
        })
        that.getVersion()
      } else {
        that.setData({
          grade: that.data.tempgrade,
          categray_type: that.data.tempcategray_type,
          version: id,
          chooseVersionText:title

        })
        that.getChooseData();
      }
    }, 200)



  },
  chooseBg: function () {

  },
  /**
   * 点击选择类型
   */
  chooseType: function () {
    let that = this
    var id = that.data.categray_type
    that.setData({
      msg: false,
      chooseData: that.data.categray_con,
      chooseCurrentPosition: that.data.categray_type,
      chooseType: 1,
      chooseCurrentPosition: id
    })

  },
  /**
   * 点击年级类型
   */
  chooseLevel: function () {
    let that = this
    if (that.data.tempcategray_type == -1) {
      wx.showToast({
        title: "请先选择课程类型",
        icon: 'none',
        duration: 2000
      })
      return
    }
    that.setData({
      msg: false,
      chooseData: that.data.grade_con[that.data.categray_type],
      chooseType: 2
    })
    console.log("chooseData:" + chooseData[0].title)
  },
  /**
   * 点击版本类型
   */
  chooseVesion: function () {
    let that = this
    if (that.data.tempcategray_type == -1) {
      wx.showToast({
        title: "请先选择课程类型",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (that.data.tempgrade == -1) {
      wx.showToast({
        title: "请先选择年级",
        icon: 'none',
        duration: 2000
      })
      return
    }
    that.setData({
      msg: false,
      chooseData: that.data.versionlist,
      chooseType: 3
    })
    that.getVerdion();
  },
  /**
   * 查看课程详情
   */
  lookDetail: function (e) {
    var id = e.currentTarget.dataset['id'];
    //跳转到课程详情界面
   //查看详情
   wx.navigateTo({
    url: '/pages/coreseDetail/coreseDetail?id=' + id,
  })
  },
})